<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * Run sql queries to insert sample content
 *
 * @package GeoContexter
 * @subpackage Module_System
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev$ / $LastChangedDate$ / $LastChangedBy$
 */

namespace Setup\Model;

class InsertSampleContent
{
    public function run( $data )
    {
        $_sql_file = GEOCONTEXTER_ROOT . '/module/Setup/sql/test.sql';

        if(false !== ($_sql_content = file_get_contents($_sql_file)))
        {

                if(false === $data['db']->exec($_sql_content))
                {
                    $_message =  array('error_source' => $_sql_file,
                                       'pg_info'      => $data['db']->errorInfo(),
                                       'query'        => '');

                    throw new \Exception('Setup sql error: ' .
                                         var_export($_message,true));
                }
                else
                {
                    if(false === $data['db']->query("SELECT gc_create_random_records(300,
                                                                                     1000000000000001,
                                                                                     1000000000000002,
                                                                                     49.39679394621057,
                                                                                     6.008975830089846,
                                                                                     49.53958753295286,
                                                                                     6.750552978394483)"))
                    {
                        $_message =  array('error_source' => 'pg function geocontexter.gc_create_random_records',
                                           'pg_info'      => $data['db']->errorInfo(),
                                           'query'        => '');

                        throw new \Exception('Setup sql error: ' .
                                             var_export($_message,true));
                    }
                }
        }
        else
        {
            throw new \Exception('File dosent exists: ' . $_sql_file);
        }
    }
}
